<?php

/**
 * This is the model class for table "{{msg_log}}".
 *
 * The followings are the available columns in table '{{msg_log}}':
 * @property integer $zml_id
 * @property integer $zm_id_from
 * @property integer $zm_id_to
 * @property integer $zml_type
 * @property integer $zml_theme
 * @property string $zml_title
 * @property string $zml_content
 * @property integer $zml_reply_id
 * @property string $zml_reply
 * @property string $zml_tel
 * @property string $zml_qq
 * @property string $zml_email
 * @property string $zml_address
 * @property integer $zml_status
 * @property integer $inputtime
 * @property integer $updatetime
 * 张梦林 
 * 2012-12-5
 */
class MsgLog extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return MsgLog the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{msg_log}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zm_id_from, zm_id_to, zml_type, zml_theme, zml_reply_id, zml_status, inputtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('zml_title', 'length', 'max'=>100),
			array('zml_tel', 'length', 'max'=>20),
			array('zml_qq', 'length', 'max'=>15),
			array('zml_email, zml_address', 'length', 'max'=>255),
			array('zml_content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zml_id, zm_id_from, zm_id_to, zml_type, zml_theme, zml_title, zml_content, zml_reply_id, zml_tel, zml_qq, zml_email, zml_address, zml_status, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'fromMember'=>array(self::BELONGS_TO,'Member',array('zm_id_from'=>'zm_id')),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zml_id' => 'ID',
			'zm_id_from' => '发信息的会员ID',
			'zm_id_to' => '收信息的会员ID',
			'zml_type' => '信息类型',//信息类型:1用户,2系统'
			'zml_theme' => '主题/回复',
			'zml_title' => '标题',
			'zml_content' => '内容',
			'zml_reply_id' => '回复ID',
			'zml_tel' => '手机或电话',
			'zml_qq' => 'QQ号码',
			'zml_email' => '邮箱',
			'zml_address' => '地址',
			'zml_status' => '状态',
			'inputtime' => '创建时间',
			'updatetime' => '更新时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
	
		$criteria=new CDbCriteria;
	
		$criteria->compare('zml_id',$this->zml_id);
		$criteria->compare('zm_id_from',$this->zm_id_from);
		$criteria->compare('zm_id_to',$this->zm_id_to);
		$criteria->compare('zml_type',$this->zml_type);
		$criteria->compare('zml_theme',$this->zml_theme);
		$criteria->compare('zml_title',$this->zml_title,true);
		$criteria->compare('zml_content',$this->zml_content,true);
		$criteria->compare('zml_reply_id',$this->zml_reply_id);
		$criteria->compare('zml_tel',$this->zml_tel,true);
		$criteria->compare('zml_qq',$this->zml_qq,true);
		$criteria->compare('zml_email',$this->zml_email,true);
		$criteria->compare('zml_address',$this->zml_address,true);
		$criteria->compare('zml_status',$this->zml_status);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);
	
		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function loadMyMsg()
	{
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id_to',Yii::app()->user->userid);
		$criteria->compare('zml_status',array(2,3,4));
		$criteria->order = " inputtime DESC ";
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 返回未读信息
	 */
	public function unreadAmount()
	{
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id_to',Yii::app()->user->userid);
		$criteria->compare('zml_status',3);
		return $this->count($criteria);
	}
	/*
	 * 返回系统类型
	 * */
	public function getType($type=null){
		if($type==null){
			return array(
					'1'=>'用户',
					'2'=>'系统',
			);
		}else{
			$level = $this->getType();
			return $level[$type];
		}
	}
	/*
	 * 返回主题
	 * */
	public function getTheme($type=null){
		if($type==null){
			return array(
					'1'=>'主题',
					'2'=>'回复',
			);
		}else{
			$level = $this->getTheme();
			return $level[$type];
		}
	}
	/*
	 * 返回审核状态
	 * */
	public function getStatus($type=null){
		if($type==null){//1未审核2已审核3未查看4已查看,5删除
			return array(
					'1'=>'未审核',
					'2'=>'已审核',
					'3'=>'未查看',
					'4'=>'已查看',
					'5'=>'删除',
			);
		}else{
			$level = $this->getStatus();
			return $level[$type];
		}
	}
	/*
	* 发起系统信息
	* @param $zm_id 收信息用户ID
	* @param $title 标题
	* @param $content 内容
	* */
	public function sendSystemInfo($zm_id,$title,$content){
		$this->__construct('insert');
		$this->zm_id_from = 0;
		$this->zm_id_to = $zm_id;
		$this->zml_type = 2; //信息类型为系统
		$this->zml_theme =1; //主题为主题
		$this->zml_title = $title;
		$this->zml_content = $content;
		$this->zml_status = 3; //状态为未查看
		$this->inputtime = time();
		$this->insert(array('zm_id_from','zm_id_to','zml_type','zml_theme','zml_title','zml_content','zml_status','inputtime'));
	}
	/*
	 * 发送会员消息
	 * @param $Zm_Id  收信息用户ID
	 * @param $content 内容
	 * */
	public function sendMemberInfo($zm_id,$content){
		$model = new MsgLog();
		$model->zm_id_from = Yii::app()->user->userid;
		$model->zm_id_to = $zm_id;
		$model->zml_type = 1; //信息类型为用户
		$model->zml_theme =1; //主题为主题
		$model->zml_content = $content;
		$model->zml_status = 3; //状态为未查看
		$model->inputtime = time();
		$model->save();
	}
	/*
	 * 回复会员消息
	 * @param $zml_id 要回复的消息ID
	 * @param $content 回复的内容
	 * */
	public function replyMsgInfo($zml_id,$content){
		$msginfo = $this->findByPk($zml_id);
		//创建回复信息
			$this->__construct('insert');
			$this->zml_theme = 2; //主题为回复
			$this->zml_content = $content;
			$this->zml_status = 3; //状态为未查看
			$this->inputtime = time();
			$this->insert(array('zml_theme','zml_content','zml_status','inputtime'));
		//更新信息，加入回复ID
		$replyid = $this->getPrimaryKey();
			$this->__construct('insert');
			$this->zml_id = $zml_id;
			$this->zml_reply_id = $replyid->zml_id;
			$this->updatetime = time();
			$this->save(array('zml_id','zml_reply_id','updatetime'));
	}
	
	/**
	 * 会员删除信息
	 * $idbox id数组
	 * $memberid 会员id
	 * */
	public function deleteMsg($idbox, $memberid=null){
		$condition = 'zml_id in('.implode(',', $idbox).')';
		if($memberid){
			$condition .= ' and zm_id_to='.$memberid;
		}
		$messages = $this->findAll($condition);
		return $this->updateAll(array('zml_status'=>5),$condition);
	}
	
	/*
	 * 逻辑删除
	 * */
	public function logicDelete($id){
		$model = $this->findByPk($id);
		$model->zml_status = 5;
		$model->save();
	}

	//未读消息条数
	public function getMsgLogNum(){
		$model = MsgLog::model();
		if(isset(Yii::app()->user->userid)){
			$condition = 'zml_status=3 and zm_id_to='.Yii::app()->user->userid;
			$num = $model->count($condition);
		}else{
			$num = 0;
		}
		return $num;
	}

}